// pages/index/component/foodList/foodList.js
import {
  list,recommendTagList
} from '../../../../api/recommend'
import util from '../../../../utils/util'
const app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    info: {
      type: Object
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    list: [],
    leftList:[],
    rightList:[],
    pageNum: 1,
    find:[1,2,3,4,5],
    tagIds:""
  },


  /**
   * 组件的方法列表
   */
  lifetimes: {
    attached() {
      var _this = this
      var arr = Object.keys(this.data.info.tagids)
		  if (arr.length > 0) {
			this.data.tagIds = this.data.info.tagids.split(',')
			console.log(this.data.tagIds)
			for (var i = 0; i < this.data.tagIds.length; i++) {
			  this.data.tagIds[i] = parseInt(this.data.tagIds[i])
			}
			this.setData({
			  tagIds: this.data.tagIds
			})
      }
      this.loadList()
      this.data.list.forEach(item => {
        if (item.type == 1 || item.type == 3) {
          wx.getImageInfo({
            src: item.shopVo.logo,
            success: function (res) {
              item.height = res.height + 'px'
            }
          })
        } else if (item.type == 0) {
          wx.getImageInfo({
            src: item.productVo.thumbnail,
            success: function (res) {
              item.height = res.height + 'rpx'
            }
          })
        }
      })
      _this.setData({
        list: this.data.list
      })
    },
  },
  methods: {
    onReachBottom() {
      this.data.pageNum += 1
      this.loadList()
    },
    downRefresh() {
      this.data.pageNum = 1
      this.data.list = []
      this.loadList()
    },
    toDetail(data) {
      console.log(data);
      if (data.currentTarget.dataset.type == 1 ) {
        wx.navigateTo({
          url: '/pages/shop/shopDetail/index?shopId=' + data.currentTarget.dataset.id +
            '&mchId=' + data.currentTarget.dataset.mchid
        })
      }else if(data.currentTarget.dataset.type == 3){
        wx.navigateTo({
          url: '/pages/cook/kitchenBrigade/kitchenBrigade?id=' + data.currentTarget.dataset.id 
            
        })
      } else {
        wx.navigateTo({
          url: '/pages/shop/productDetail/productDetail?productId=' + data.currentTarget.dataset.id
        })
      }

    },
    loadList() {
      // list
      recommendTagList({
        tagIds: this.data.tagIds,
        pageNum: this.data.pageNum,
        lat: app.globalData.lat,
        lng: app.globalData.lng
      }).then(res => {
        if (res.data.data.length > 0) {
          res.data.data.forEach(item => {
            item.distance = util.rateConverter(item.distance)
            if (item.type == 1 || item.type == 3) {
              item.shopVo.promotions1=[]
              item.shopVo.promotions2=[]
              if(item.shopVo.promotions.length>0){
                item.shopVo.promotions.forEach(item1=>{
                    if(item1.type==3){
                      item.shopVo.promotions1.push(item1)
                    }else{
                      item.shopVo.promotions2.push(item1)
                    }
                })
              }
              if (util.isNull(item.shopVo.monthSale)) {
                item.shopVo.monthSale = 0
              }
              if (util.isNull(item.shopVo.score)) {
                item.shopVo.score = 0
              }
              if (!util.isNull(item.shopVo.logo)) {
                item.shopVo.logo = util.thumbnail_cover(item.shopVo.logo, 347)
              }
            } else {
              if (!util.isNull(item.productVo.thumbnail)) {
                item.productVo.thumbnail = util.thumbnail_cover(item.productVo.thumbnail, 347)
              }
            }
            this.data.list.push(item)
          })
          this.data.list.sort((a,b)=>b.orders - a.orders)
          this.setData({
            list: this.data.list
          })
          console.info("甄选推荐的列表--",this.data.list)
          setTimeout(() => {
            this.triggerEvent('getCanHeight')
          }, 500)
        }
      })
    },
  }
})